﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Test_QianBZ.DataAccess;
using Test_QianBZ.Models;

namespace Test_QianBZ.Repository
{
    public class PersonRepository
    {
        public PersonModel GetPerson(int personId)
        {
            var db = new eatContext();
            var person = db.TbPerson
                .Where(s => s.PersonId == personId)
                .Select(s=>new PersonModel { PersonId = s.PersonId, PersonName = s.PersonName })
                .FirstOrDefault();
            return person;
        }

        public List<PersonModel> GetAllPerson()
        {
            var db = new eatContext();
            var person = db.TbPerson
                .Select(s => new PersonModel {
                    PersonId = s.PersonId,
                    PersonName = s.PersonName })
                .ToList();
            return person;
        }

        public int AddPerson(string personName)
        {
            var db = new eatContext();
            var tbperson = new TbPerson
            {
                PersonName = personName
            };
            db.TbPerson.Add(tbperson);
            return (db.SaveChanges());
        }
    }

}
